//
//  LTM_Request.m
//  LTM_Lib_Network_Objc
//
//  Created by 柯南 on 2021/12/7.
//

#import "LTM_Request.h"

#import "LTM_NetworkConfig.h"
#import "LTM_DataDeal.h"

#import <YTKNetwork/YTKNetworkAgent.h>
#import <YYModel/YYModel.h>

@implementation LTM_Request

- (void)start
{
    YTKNetworkAgent *agent = [YTKNetworkAgent sharedAgent];
    NSSet *acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain", @"text/html", nil];
    NSString *keypath = @"jsonResponseSerializer.acceptableContentTypes";
    [agent setValue:acceptableContentTypes forKeyPath:keypath];
    
    [super start];
}

- (YTKRequestSerializerType)requestSerializerType {
    return YTKRequestSerializerTypeJSON;
}

- (YTKResponseSerializerType)responseSerializerType {
    return YTKResponseSerializerTypeJSON;
}

- (NSDictionary *)requestHeaderFieldValueDictionary {
    return [LTM_NetworkConfig sharedConfig].headerDic;
}

- (id)requestArgument {
    if (self.param) {
        return self.param;
    }
    return nil;
}

- (void)requestData:(Class)className
            Success:(void(^)(NSObject *model))success
            Failure:(void(^)(NSError *error))failure{
    [self startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        if (success) {
            [LTM_DataDeal requestDealSuccess:request.responseObject Target:className Success:success Failure:failure];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [LTM_DataDeal requestDealFailure:request.error Failure:failure];
    }];
}

- (NSString *)requestUrl {
    return self.url;
}

@end
